<template>
  <div>
    <van-nav-bar title="账号登录" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>

    <router-link class="box" to="/register">还没有账号，去注册~</router-link>
  </div>
</template>

<script>
import { login } from '@/api/user.js'
import { setToken } from '@/utils/storage.js'
export default {
  name: '',
  props: {},
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      const data = await login(values)
      // console.log(data.body.token)
      if (!data.body.token) {
        return this.$toast.fail('账号或密码错误')
      } else {
        setToken(data.body.token)
        this.$router.push('/')
      }
      // setToken(data.body.token)
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped lang="less">
.box {
  margin-left: 30vw;
}
</style>
